<template>
  <div class="problem">
    <el-card>
      <!-- 头部关键字 -->
      <div class="guanjianzi">
        <el-row>
          <el-col :span="18">
            <el-form
              label-width="80px"
              inline
            >
              <el-form-item label="关键字">
                <el-input
                  class="aull"
                  v-model="keyword"
                  placeholder="根据编号搜索"
                  size="small"
                ></el-input>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col
            :span="6"
            style="text-align: right"
          >
            <el-button
              size="small"
              @click="keyword=null"
            >清除</el-button>
            <el-button
              type="primary"
              size="small"
              @click="succest"
            >搜索</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 消息提示 -->
      <el-alert
        type="info"
        show-icon
        :closable="false"
      >
        <template slot="title">数据一共 {{zull}} 条</template>
      </el-alert>
      <!-- 表格 -->
      <el-table
        :data="list"
        style="width: 100%"
        class="lists"
      >
        <el-table-column
          prop="id"
          label="编号"
          width="220"
        >
        </el-table-column>
        <el-table-column
          label="题型"
          width="100"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.questionType == 1">简答</span>
            <span v-else-if="scope.row.questionType == 2">单选</span>
            <span v-else>多选</span>
          </template>
        </el-table-column>

        <el-table-column
          label="题目编号"
          width="220"
        >
          <template slot-scope="scope">
            <div
              class="topic"
              v-for="(it,i) in scope.row.questionIDs"
              :key="i"
              @click="PopoverOnclick(it)"
            >
              {{it.number}}
            </div>

          </template>
        </el-table-column>

        <el-table-column
          prop="addTime"
          label="录入时间"
          width="220"
        >
        </el-table-column>
        <el-table-column
          prop="totalSeconds"
          label="答题时间(s)"
          width="270"
        >
        </el-table-column>
        <el-table-column
          prop="accuracyRate"
          label="正确率(%)"
          width="260"
        >
        </el-table-column>
        <el-table-column
          prop="userName"
          label="录入人"
          width="260"
        >
        </el-table-column>
        <el-table-column
          prop="date"
          label="操作"
          width="100"
        >
          <template slot-scope="scope">
            <el-button
              class="anniu"
              type="danger"
              icon="el-icon-delete"
              round
              plain
              @click="shanchu(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        class="fenye"
        background
        :current-page="page"
        :page-size="pagesize"
        layout="prev, pager, next, sizes, jumper"
        :page-sizes="[20, 30, 50, 100]"
        :total="zull"
      >
        >
      </el-pagination>
    </el-card>
    <popover
      v-if="show"
      :idshow='idshow'
      v-model="show"
    />
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions'
import Popover from '@/module-hmmm/components/questions-preview'

export default {
  // 组件名称
  name: 'Problem',
  // 局部注册的组件
  components: {
    Popover
  },
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
    return {
      list: [], // 获取的数据列表
      page: 1,
      pagesize: 20,
      keyword: null,
      zull: 0,
      show: false,
      idshow: {}

    }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 生命周期钩子   注：没用到的钩子请自行删除
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    this.succest()
  },
  /**
  * el 被新创建的 vm.el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.el 也在文档内。
  */
  mounted () { },
  // 组件方法
  methods: {
    async succest () {
      const { data } = await randoms({
        page: this.page,
        pagesize: this.pagesize,
        keyword: this.keyword
      })
      this.list = data.items
      this.zull = data.counts
      // console.log(this.list)
      // console.log(data)
    },
    // 监听 pagesize 改变的事件
    handleSizeChange (newSize) {
      // console.log(newSize)
      this.pagesize = newSize
      this.succest()
    },
    // 监听页码值改变的事件
    handleCurrentChange (newPage) {
      // console.log(newPage)
      this.page = newPage
      this.succest()
    },
    // 删除按钮事件
    async shanchu (stat) {
      const confirmResult = await this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 如果用户确认删除,则返回值为字符串 confirm
      // 如果用户取消删除,则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消了删除')
      }
      // console.log(stat.id)
      await removeRandoms(stat)
      this.$message.success('删除成功')
      this.succest()
    },
    // 点击触发弹出层
    PopoverOnclick (row) {
      // console.log(row)
      this.idshow = row
      this.show = true
    }
    // 控制弹出层的显示与隐藏
  }
}
</script>

<style scoped lang="scss">
.problem {
  padding: 10px 15px;
}
.aull {
  width: 400px;
}
.lists {
  margin: 15px 0;
}
.anniu {
  width: 32px;
  height: 32px;
}
.el-button--medium.is-round {
  padding: 0;
}
.fenye {
  text-align: end;
}
.topic {
  cursor: pointer;
  color: rgb(0, 165, 254);
}
</style>
